# point pattern reconstruction based on P(n,r), where r is the distance away
# from a focal point (and a location) and n is the number of points within 
# distance r
# 
# data.ppp: observed point pattern
#     covr: covariates used to generate the start random point pattern, and
#           determine the probability of a point death and regenerate, in 
#           which the importance of the covrs will decrease if the rejection
#			rate is high.
# controls:  control parameters of the compared probability surface P(n,r).
#   npmax:  maximum percentage of individuals in P(n,r)
#   npbin:  steps of percentage of individual axis in P(n,r)
#    rmax:  maximum distance in P(n,r)
#    rbin:  steps of observational distance in P(n,r)
#    tolerance: the minimum errors the algrothim should achived
#    Ngiveup: maximum number of tries before achive the tolerance.     
#
# Note: current version is runing under none edge correction.
#
# Author: Guochun Shen
# Data:   2011-12-19
# Project:spatial statistic
# Email:  shenguochun@gmail.com
###############################################################################

rec_pattern=function(data.ppp,covr=NULL,
		controls=list("npmax"=1,"npbin"=0.01,
				"rmax"=80,"bin"=1,"tolerance"=5e-3,"Ngiveup"=1e+4)){
	pairwise_dist_obs=pairdist(data.ppp)
	pnr_surface_obs=pnr_surface(pairwise_dist,controls)
	xy=burning(data.ppp$n,data$win,pairwise_dist_obs,pnr_surface_obs,covr,controls)
	rec_data=ppp(x=xy[,1],y=xy,window=data.ppp$win,check=FALSE)
}

